/*
function ccc(text) {
        alert(text)
}
export default ccct;*/

import Environment from "../../../plugins/utils/environment";
import loadJson from "../../utils/loadJson";
import $ from "jquery";
function retrieval() {}

retrieval.findPosition = function(retrievalSubstance, that, viewer, retrievalSubstance_array) {
    if (retrievalSubstance.length > 0) {
        let lat = null;
        let lon = null;
        const floatlatregex = /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)$/;
        /* const floatlonregex = /^[-+]?((1[0-7]\d{1}|0?\d{1,2})\.\d{1,5}|180\.0*)$/;*/
        const floatlonregex = /^-?((0|1?[0-8]?[0-9]?)(([.][0-9]{1,10})?)|180(([.][0]{1,10})?))$/;

        const stringregex = /^[\u4e00-\u9fa5]{2,}$/;
        if (retrievalSubstance.indexOf("，") !== -1 || hasLetter(retrievalSubstance) === true) {
            /*alert("包含非法字符");*/
            that.$message({
                showClose: true,
                message: "包含非法字符",
                type: "error"
            });
            return;
        }
        if (viewer.entities.getById("retrieval") !== undefined) {
            let m_ponitid = viewer.entities.getById("retrieval");
            viewer.entities.remove(m_ponitid);
        }

        if (retrievalSubstance.indexOf(",") !== -1) {
            lon = parseFloat(retrievalSubstance.split(",")[0]);

            lat = parseFloat(retrievalSubstance.split(",")[1]);
            if (!floatlonregex.test(retrievalSubstance.split(",")[0])) {
                /*    alert("经度填写错误");*/
                that.$message({
                    showClose: true,
                    message: "经度填写错误",
                    type: "error"
                });
                return;
            }
            if (!floatlatregex.test(retrievalSubstance.split(",")[1])) {
                /*   alert("纬度填写错误");*/
                that.$message({
                    showClose: true,
                    message: "纬度填写错误",
                    type: "error"
                });
                return;
            }

            let pointa = viewer.entities.add({
                id: "retrieval",
                position: Cesium.Cartesian3.fromDegrees(lon, lat),
                label: {
                    //文字标签
                    text: retrievalSubstance,
                    font: "24px Helvetica",
                    fillColor: Cesium.Color.SKYBLUE,
                    outlineColor: Cesium.Color.BLACK,
                    outlineWidth: 2,
                    disableDepthTestDistance: Number.POSITIVE_INFINITY,
                    style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                    horizontalOrigin: Cesium.HorizontalOrigin.RIGHT,
                    verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                    //中心位置
                    pixelOffset: new Cesium.Cartesian2(retrievalSubstance.length * 5, -10)
                },
                point: {
                    pixelSize: 12,
                    color: Cesium.Color.RED,
                    outlineColor: Cesium.Color.WHITE,
                    outlineWidth: 2,
                    horizontalOrigin: Cesium.HorizontalOrigin.RIGHT,
                    verticalOrigin: Cesium.VerticalOrigin.BOTTOM
                }
            });
            viewer.zoomTo(pointa);
            viewer.camera.flyTo({
                destination: Cesium.Cartesian3.fromDegrees(lon, lat, 1000000.0) //相机飞入点
            });
        } else {
            $.ajaxSettings.async = false;
            loadJson("file/locationinformation.geojson", function(data) {
                retrievalSubstance_array = data;
            });
            $.ajaxSettings.async = true;
            if ([] === retrievalSubstance_array || null === retrievalSubstance_array || "" === retrievalSubstance_array || 0 === retrievalSubstance_array.length) {
                that.$message({
                    showClose: true,
                    message: "暂无法获取数据",
                    type: "error"
                });
                return;
            } else {
                if (!stringregex.test(retrievalSubstance)) {
                    /*alert("输入非法字符，请输入至少两个汉字");*/
                    that.$message({
                        showClose: true,
                        message: "输入非法字符，请输入至少两个汉字",
                        type: "error"
                    });
                    return;
                } else {
                    for (let i = 0; i < retrievalSubstance_array.length; i++) {
                        if (retrievalSubstance_array[i].sheng.indexOf(retrievalSubstance) !== -1) {
                            lon = parseFloat(retrievalSubstance_array[i].lon);
                            lat = parseFloat(retrievalSubstance_array[i].lat);
                            break;
                        } else if (retrievalSubstance_array[i].shi.indexOf(retrievalSubstance) !== -1) {
                            lon = parseFloat(retrievalSubstance_array[i].lon);
                            lat = parseFloat(retrievalSubstance_array[i].lat);
                            break;
                        } else if (retrievalSubstance_array[i].xian.indexOf(retrievalSubstance) !== -1) {
                            lon = parseFloat(retrievalSubstance_array[i].lon);
                            lat = parseFloat(retrievalSubstance_array[i].lat);
                            break;
                        }
                    }
                    if (lon == null && lat == null) {
                        that.$message({
                            showClose: true,
                            message: "未查到数据",
                            type: "error"
                        });
                        /*  alert("未查到数据");*/
                        return;
                    } else {
                        var point = viewer.entities.add({
                            id: "retrieval",
                            position: Cesium.Cartesian3.fromDegrees(lon, lat),
                            label: {
                                //文字标签
                                text: retrievalSubstance,
                                font: "24px Helvetica",
                                fillColor: Cesium.Color.SKYBLUE,
                                outlineColor: Cesium.Color.BLACK,
                                outlineWidth: 2,
                                disableDepthTestDistance: Number.POSITIVE_INFINITY,
                                style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                                horizontalOrigin: Cesium.HorizontalOrigin.RIGHT,
                                verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                                //中心位置
                                pixelOffset: new Cesium.Cartesian2(20, -10)
                            },
                            point: {
                                pixelSize: 12,
                                color: Cesium.Color.RED,
                                outlineColor: Cesium.Color.WHITE,
                                outlineWidth: 2,
                                horizontalOrigin: Cesium.HorizontalOrigin.RIGHT,
                                verticalOrigin: Cesium.VerticalOrigin.BOTTOM
                            }
                        });
                        viewer.zoomTo(point);

                        viewer.camera.flyTo({
                            destination: Cesium.Cartesian3.fromDegrees(lon, lat, 1000000.0) //相机飞入点
                        });
                    }
                }
            }
        }
    } else {
        that.$message({
            showClose: true,
            message: "内容不能为空",
            type: "error"
        });
    }

    //判断是否包含英文
    function hasLetter(str) {
        for (var i in str) {
            var asc = str.charCodeAt(i);
            if ((asc >= 65 && asc <= 90) || (asc >= 97 && asc <= 122)) {
                return true;
            }
        }
        return false;
    }
};

export default retrieval;
